package com.ec.as.controller;

import com.ec.as.domain.dto.LeaveRequestDTO;
import com.ec.as.domain.dto.LeaveRequestQueryDTO;
import com.ec.as.domain.po.LeaveRequest;
import com.ec.as.service.LeaveRequestService;
import com.ec.common.domain.PageVO;
import com.ec.common.domain.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

@RestController
@RequestMapping("/leave")
@Api(tags = "请假申请管理")
public class LeaveRequestController {

    @Resource
    private LeaveRequestService leaveRequestService;

    @ApiOperation(value = "提交请假申请", notes = "老人提交请假申请")
    @PostMapping("/submit")
    public Result<LeaveRequest> submitLeaveRequest(@Valid @RequestBody LeaveRequestDTO dto) {
        return leaveRequestService.submitLeaveRequest(dto);
    }

    @ApiOperation(value = "取消待审核的请假申请", notes = "老人取消待审核的请假申请")
    @PostMapping("/cancel/pending/{id}")
    public Result<Void> cancelPendingLeaveRequest(@PathVariable Long id) {
        return leaveRequestService.cancelPendingLeaveRequest(id);
    }

    @ApiOperation(value = "提交取消已批准的请假申请", notes = "老人提交取消已批准的请假申请，需管理员审核")
    @PostMapping("/cancel/approved/{id}")
    public Result<Void> submitCancelApprovedLeaveRequest(@PathVariable Long id) {
        return leaveRequestService.submitCancelApprovedLeaveRequest(id);
    }

    @ApiOperation(value = "管理员审核请假申请", notes = "管理员审核请假申请，approve=true为通过，false为拒绝")
    @PostMapping("/review/{id}")
    public Result<Void> reviewLeaveRequest(@PathVariable Long id,
                                          @RequestParam boolean approve,
                                          @RequestParam Integer adminId) {
        return leaveRequestService.reviewLeaveRequest(id, approve, adminId);
    }

    @ApiOperation(value = "管理员审核取消申请", notes = "管理员审核取消申请，approve=true为通过取消，false为拒绝取消")
    @PostMapping("/review/cancel/{id}")
    public Result<Void> reviewCancelRequest(@PathVariable Long id,
                                           @RequestParam boolean approve,
                                           @RequestParam Integer adminId) {
        return leaveRequestService.reviewCancelRequest(id, approve, adminId);
    }

    @ApiOperation(value = "查询请假申请列表", notes = "支持分页和筛选")
    @GetMapping("/list")
    public Result<PageVO<LeaveRequest>> listLeaveRequests(@ModelAttribute LeaveRequestQueryDTO queryDTO) {
        return leaveRequestService.listLeaveRequests(queryDTO);
    }
}